/**
 * 
 */
package game.rwalk.control;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import game.rwalk.utility.Constants;
import game.rwalk.utility.ProccessFile;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import game.rwalk.R;
import game.rwalk.entity.Player;

/**
 * @author sk8erboi
 *
 */
public class MainGame extends BaseGame {
	
	private Carousel carousel;
	private SelectColorGame selectColorGame;
	private StatusGameScreen statusGameScreen;
	private SelectPlayerScreen selectPlayer;
	private Context context;
	
	private ProccessFile file;
	
	//game variables
	
	private ArrayList<Player> players = new ArrayList<Player>();
	private String[] names = new String[] { "Dao Duc Thang", "Nguyen Cao Cuong", "Ngo Van Phu", "Truong Duc Nhat"};
	
	//game control///////////////////////////////////////////////////
	boolean isSelectColor = false,
			isPlay = false,
			isUpdateStatusGame = false,
			isGameOver = false,
			isSelectedPlayer = false;
	/////////////////////////////////////////////////////////////////
	
	private Paint bitmapPaint = new Paint();

	public MainGame(Context context) {
		super(context);
		this.context = context;
		super.setUpdatePeriod(Constants.DELAY);
	}
	
	public MainGame(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		super.setUpdatePeriod(Constants.DELAY);
	}

	@Override
	protected void updatePhysics() {
		
		if(!isGameOver) {
			carousel.checkOutScope();
		}
		
		if(isUpdateStatusGame && !isGameOver) {
			updateGameStatus();
			isUpdateStatusGame = false;
		}
		
		if(isSelectedPlayer) {
			statusGameScreen.player = players.get(selectPlayer.indexPlayer).name;
		}
		
		if(isGameOver) {
			players.get(selectPlayer.indexPlayer).score = statusGameScreen.score;
			isSelectedPlayer = false;
			isGameOver = false;
			isPlay = false;
			isSelectColor = false;
			
			carousel.init();
			
			statusGameScreen.score = 0;
			statusGameScreen.miss = 0;
		}
	}
	
	private void updateGameStatus() {
		
		if(carousel.hit) {
			System.out.println(carousel.hit);
			if(carousel.getHitColor() == selectColorGame.selectedColor) {
				statusGameScreen.score += Constants.POINT;
				statusGameScreen.miss = 0;
			} else {
				statusGameScreen.miss++;
			}
		}
		
		if(statusGameScreen.miss == 5) {
			isGameOver = true;
		}
	}

	int[] ids, index;
	@Override
	protected void initialize() {
		
		// Load fire image sprites
		ids = new int[] { R.drawable.yellow, R.drawable.blue, R.drawable.red,
						  R.drawable.yellow2, R.drawable.blue2, R.drawable.red2
						};
		index = new int[] {0, 1, 2};
		
		//4 players
		for(int i = 0; i < 4; i++) {
			Player player = new Player();
			player.name = names[i];
			player.score = 0;
			player.order = 0;
			
			players.add(player);
		}
		
		//
		selectPlayer = new SelectPlayerScreen(players, context);
		
		//instance of StatusGameScreen
		statusGameScreen = new StatusGameScreen(context);
		
		//instance of SelectColorGame
		selectColorGame = new SelectColorGame(ids, index, context);
		
		//instance of Carousel and init
		carousel = new Carousel(ids, index, context);
		carousel.init();
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		paint(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		int eventAction = event.getAction();
		
		if(eventAction == MotionEvent.ACTION_UP) {
			if(!isSelectedPlayer) {
				isSelectedPlayer = selectPlayer.selectPlayer((int) event.getX(), (int) event.getY());
			}
			
			if( isSelectColor && isPlay) {
				carousel.hitRow((int) event.getX(), (int) event.getY());
				isUpdateStatusGame = true;
			}
			
			if(!isSelectColor && selectColorGame.hitColor((int) event.getX(), (int) event.getY())) {
				isSelectColor = true;
			}
		}
	
		return true;
	}

	private void paint(Canvas canvas) {
		
		if(!isSelectedPlayer) {
			selectPlayer.onDraw(canvas);
			
		}
		
		if(!isSelectColor && isSelectedPlayer) {
			
			selectColorGame.onDraw(canvas);
			
			//set game status
			isPlay = true;
			
			
		} else if(isSelectColor && isPlay && !isGameOver) {
			
			carousel.onDraw(canvas, bitmapPaint);	
		} else if(isPlay && isGameOver) {
			
			//draw gameover
			
			
		} else {
			//back to main menu
			
		}
		
		if(!isGameOver && isSelectedPlayer) {
			
			statusGameScreen.onDraw(canvas);
		}
		
	}

	@Override
	protected boolean gameOver() {
		
		return false;
	}

	@Override
	protected long getScore() {
		// TODO Auto-generated method stub
		return 0;
	}
}
